<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>新增用户</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		<link rel="stylesheet" href="./css/frame/font.css">
		<link rel="stylesheet" href="./css/frame/xadmin.css">
		<script type="text/javascript" src="./lib/jquery/jquery-3.3.1.min.js"></script>
		<script type="text/javascript" src="./lib/layui/layui.js"></script>
		<script type="text/javascript" src="./js/frame/xadmin.js"></script>
		<script type="text/javascript" src="./js/constant/urls.js"></script>
	</head>

	<body>
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			<legend>添加用户信息</legend>
		</fieldset>

		<form class="layui-form" action="">
			<div class="layui-form-item">
				<div class="layui-inline" style="width: 31.25rem">
					<label class="layui-form-label">登录名</label>
					<div class="layui-input-inline">
						<input type="text" name="loginName" id="loginName" lay-verify="required" autocomplete="off" placeholder="请输入用户登录名"
						 class="layui-input" style="width: 18.75rem;">
					</div>
				</div>
				<div class="layui-inline" style="width: 31.25rem">
					<label class="layui-form-label">手机号</label>
					<div class="layui-input-inline">
						<input type="tel" name="mobilePhone" lay-verify="required|phone" autocomplete="off" placeholder="请输入用户手机号" class="layui-input"
						 style="width: 18.75rem;">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline" style="width: 31.25rem">
					<label class="layui-form-label">登录密码</label>
					<div class="layui-input-inline">
						<input type="password" name="password" lay-verify="required|pass" placeholder="请输入密码" autocomplete="off" class="layui-input"
						 style="width: 18.75rem;">
					</div>
				</div>
				<div class="layui-inline" style="width: 31.25rem">
					<label class="layui-form-label">确认密码</label>
					<div class="layui-input-inline">
						<input type="password" name="password" lay-verify="required|pass" placeholder="请再次输入密码" autocomplete="off" class="layui-input"
						 style="width: 18.75rem;">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline" style="width: 31.25rem">
					<label class="layui-form-label">用户名</label>
					<div class="layui-input-block">
						<input type="text" name="username" id="username" autocomplete="off" placeholder="请输入用户名" class="layui-input"
						 style="width: 18.75rem;">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">角色</label>
				<div class="layui-input-block" id="role"></div>
			</div>
			<div class="layui-upload" style="margin-left: 110px;">
				<button type="button" class="layui-btn" id="coverImageUpload">点击上传头像</button>
				<div class="layui-upload-list">
					<img class="layui-upload-img" width="240" height="240" id="coverImage">
					<p id="demoText"></p>
				</div>
			</div>
			<div class="layui-form-item layui-form-text">
				<label class="layui-form-label">个性签名</label>
				<div class="layui-input-block" style="width: 31.25rem;">
					<textarea placeholder="请输入个性签名" class="layui-textarea" name="signature" style="height: 9.375rem;"></textarea>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
		<script>
			$.ajax({
				type: "get",
				url: urls.sysUrl + "role/all",
				async: true,
				xhrFields: {
					withCredentials: true
				},
				success: function(res) {
					if (res.errorCode === 0) {
						$('#role').html("");
						for (let role of res.data) {
							$('#role').append("<input type='checkbox' name='roleIds' value='" + role.id + "' title=" + role.name + ">");
						}
					}
				}
			});
			layui.use(['form', 'upload'], function() {
				var form = layui.form,
					layer = layui.layer,
					upload = layui.upload;
				var imgPath = "";
				//图片上传
				var uploadInst = upload.render({
					elem: '#coverImageUpload',
					url: urls.fileServerUrl,
					before: function(obj) {
						//预读本地文件示例，不支持ie8
						obj.preview(function(index, file, result) {
							$('#coverImage').attr('src', result); //图片链接（base64）
						});
					},
					done: function(res) {
						//上传成功
						if (res.success) {
							imgPath = param.url + res.data;
							console.log(imgPath);
						}
					},
					error: function() {
						//演示失败状态，并实现重传
						var demoText = $('#demoText');
						demoText.html(
							'<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
						demoText.find('.demo-reload').on('click', function() {
							uploadInst.upload();
						});
					}
				});
				//监听提交
				form.on('submit(demo1)', function(data) {
					var arr_box = [];
					$('input[type=checkbox]:checked').each(function() {
						arr_box.push($(this).val());
					});
					data.field.roleIds = arr_box;
					$.ajax({
						type: "post",
						url: urls.sysUrl + 'user',
						async: true,
						data: JSON.stringify(data.field),
						contentType: "application/json; charset=utf-8",
						success: function(res) {
							if (res.errorCode === 0) {
								layer.msg(res.description, {
									icon: 6
								}, function() {
									// 获得frame索引
									var index = parent.layer.getFrameIndex(window.name);
									//关闭当前frame
									parent.layer.close(index);
								});
							} else {
								layer.msg(res.description, {
									icon: 5
								});
							}
						}
					});
					return false;
				});
			});
		</script>
	</body>

</html>
